package com.yichat.micro.msghttpservice.core.rpc;

import com.weibo.api.motan.config.springsupport.annotation.MotanService;
import com.yichat.micro.msghttpservice.common.result.HttpResult;
import com.yichat.micro.msghttpservice.common.result.OkHttpResult;
import com.yichat.micro.msghttpservice.common.result.RpcResult;
import com.yichat.micro.msghttpservice.common.vo.MsgHttpRequestVo;
import com.yichat.micro.msghttpservice.core.base.AbstractHttpCaller;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;

import javax.annotation.PostConstruct;
import java.util.HashMap;

/**
 * Created by chenmohan on 2019/04/08
 *
 * @description
 */
@MotanService
@Slf4j
public class MsgHttpServiceImpl implements MsgHttpService {

    @Autowired
    ApplicationContext applicationContext;

    @Value("${msg-http-service.protocol-http.impl}")
    String httpImpl;

    AbstractHttpCaller httpCaller;

    @Override
    public HttpResult msgHttp(MsgHttpRequestVo param) {
        HttpResult result = httpCaller.call(param);
        return result;
    }

    @Override
    public HashMap msgRpc(MsgHttpRequestVo param) {
        HashMap<String, String> resMap = RpcResult.respMap(httpCaller.call(param));
        return resMap;
    }

    @PostConstruct
    public void init() {
        log.info("【STARTING】::the impl of http framework is " + httpImpl);
        AbstractHttpCaller httpCaller = applicationContext.getBean(httpImpl, AbstractHttpCaller.class);
        this.httpCaller = httpCaller;
    }
}
